﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;
using System.Text.RegularExpressions;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     TextInfo业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class TextInfoBLL : BaseBLL
    {
        private Wsitech.Persistence.Interface.ITextInfoDao dao = null;

        private TextInfoBLL()
        {
            dao = Wsitech.Persistence.Dao.TextInfoSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static TextInfoBLL instance = null;
        private static object syncRoot = new object();
        public static TextInfoBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new TextInfoBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        /// <summary>
        /// 新闻中心ID
        /// </summary>
        public static readonly string NEWS_CENTER_ID = "e28a88d9-a653-4873-9c09-b038c521f6de";

        /// <summary>
        /// 英文版NewsCenterID
        /// </summary>
        public const string EN_NEWS_ID = "4c7fd2ab-c387-4900-8da9-a67b666ace2a";
        /// <summary>
        /// 媒体报道
        /// </summary>
        public static readonly string SITEMAPID_REPORT = "580998c2-76df-4da1-8807-4fab3e249758";
        /// <summary>
        /// 公司动态
        /// </summary>
        public static readonly string SITEMAPID_EVENTS = "9b458c66-3923-4a93-986f-eb62db2b4692";
        /// <summary>
        /// 新闻稿
        /// </summary>
        public static readonly string SITEMAPID_NEWS = "f4d900fb-e71e-4c78-96e4-68378066640b";
        /// <summary>
        /// 视频
        /// </summary>
        public static readonly string SITEMAPID_VIDEO = "f0909e9d-e4ca-4312-a994-b9e2eeaa0e8a";

        /// <summary>
        /// Press Release
        /// </summary>
        public static readonly string SITEMAPID_PRESS_RELEASE = "dd875ed3-af9f-4731-b0b2-22bee850282f";

        /// <summary>
        /// Media Reports
        /// </summary>
        public static readonly string SITEMAPID_MEDIA_REPORT = "3f0c0d27-a41a-45cf-b22d-237bf47ba743";

        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public TextInfo GetDetail(System.String textinfoid)
        {
            try
            {
                return dao.GetDetail(textinfoid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<TextInfo> GetList()
        {
            try
            {
                IList<TextInfo> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<TextInfo> GetListByParams(object TopCounts, TextInfo textinfo)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<TextInfo> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="textinfo"></param>
        /// <returns></returns>
        public object Add(TextInfo textinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Add, GetDetail(textinfo.TextInfoId));
                return dao.Add(textinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="textinfo"></param>
        /// <returns></returns>
        public int Update(TextInfo textinfo)
        {
            try
            {
                return dao.Update(textinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="TextInfoId"></param>
        /// <returns></returns>
        public int Delete(System.String textinfoid)
        {
            try
            {
                return dao.Delete(textinfoid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        #region 添加新闻信息
        /// <summary>
        /// 添加文字信息
        /// </summary>
        /// <param name="sitemapid"></param>
        /// <param name="companyid"></param>
        /// <param name="title"></param>
        /// <param name="subTitle"></param>
        /// <param name="author"></param>
        /// <param name="isTop"></param>
        /// <param name="content"></param>
        /// <param name="reference"></param>
        /// <param name="sourcePath"></param>
        /// <param name="remark"></param>
        public void AddNews(string sitemapid, string companyid, string title, string subTitle, string author, bool isTop, string content, IList<string> keywords, string reference, string sourcePath, DateTime newsPublishTime)
        {
            TextInfo info = new TextInfo();
            info.TextInfoId = Guid.NewGuid().ToString();
            info.SitemapId = sitemapid;
            info.CompanyId = companyid;
            info.Title = title;
            info.SubTitle = subTitle;
            info.Author = author;
            info.IsTop = isTop;
            info.Content = content;
            info.ReferenceUrl = reference;
            info.SourcePath = sourcePath;

            info.MediaName = "";
            info.IsFromMedia = false;
            info.MediaPublishTime = DateTime.Now;

            info.PublishTime = newsPublishTime;
            //默认禁用
            info.State = (int)UtilityEnum.State.Disable;
            info.CreateTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;

            Add(info);

            if (keywords != null && keywords.Count > 0)
            {
                TextInfoKeyRelaBLL.Instance.AddRela(info.TextInfoId, keywords);
            }
        }
        #endregion

        #region 添加视频信息
        /// <summary>
        ///  添加视频信息
        /// </summary>
        /// <param name="sitemapid"></param>
        /// <param name="companyid"></param>
        /// <param name="title"></param>
        /// <param name="keywords"></param>
        /// <param name="adder"></param>
        /// <param name="isTop"></param>
        /// <param name="description"></param>
        /// <param name="publishTime"></param>
        public void AddVideoInfo(string sitemapid, string companyid, string title, IList<string> keywords, string adder, bool isTop, string description, DateTime publishTime)
        {
            TextInfo info = new TextInfo();
            info.TextInfoId = Guid.NewGuid().ToString();
            info.SitemapId = sitemapid;
            info.CompanyId = companyid;
            info.Title = title;
            info.SubTitle = "";
            info.Author = adder;
            info.IsTop = isTop;
            info.Content = description;
            info.ReferenceUrl = "";
            info.SourcePath = "";


            //只有是媒体新闻的时候该时间才会起作用
            info.IsFromMedia = false;
            info.MediaName = "";
            info.MediaPublishTime = DateTime.Now;

            info.PublishTime = publishTime;

            //默认禁用
            info.State = (int)UtilityEnum.State.Disable;
            info.CreateTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;

            Add(info);

            //添加关键字
            if (keywords != null && keywords.Count != 0)
            {
                TextInfoKeyRelaBLL.Instance.AddRela(info.TextInfoId, keywords);
            }
        }
        #endregion

        #region 添加媒体信息

        /// <summary>
        /// 添加媒体信息
        /// </summary>
        /// <param name="sitemapid"></param>
        /// <param name="companyid"></param>
        /// <param name="title"></param>
        /// <param name="subTitle"></param>
        /// <param name="author"></param>
        /// <param name="isTop"></param>
        /// <param name="content"></param>
        /// <param name="reference"></param>
        /// <param name="sourcePath"></param>
        /// <param name="mediaPublishTime"></param>
        /// <param name="newsPublishTime"></param>
        public void AddReport(string sitemapid, string companyid, string title, string subTitle, string summary, string mediaName, string author, bool isTop, string content, IList<string> keywords, string reference, string sourcePath, DateTime mediaPublishTime, DateTime newsPublishTime)
        {
            TextInfo info = new TextInfo();
            info.TextInfoId = Guid.NewGuid().ToString();
            info.SitemapId = sitemapid;
            info.CompanyId = companyid;
            info.Title = title;
            info.SubTitle = subTitle;
            info.Summary = summary;
            info.MediaName = mediaName;
            info.Author = author;
            info.IsTop = isTop;
            info.Content = content;
            info.ReferenceUrl = reference;
            info.SourcePath = sourcePath;
            info.IsFromMedia = true;

            //只有是媒体新闻的时候该时间才会起作用
            info.MediaPublishTime = mediaPublishTime;

            info.PublishTime = newsPublishTime;

            //默认为禁用
            info.State = (int)UtilityEnum.State.Disable;
            info.CreateTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;

            Add(info);

            //添加关键字
            if (keywords != null && keywords.Count != 0)
            {
                TextInfoKeyRelaBLL.Instance.AddRela(info.TextInfoId, keywords);
            }
        }

        #endregion

        #region 根据栏目ID获取分页的信息
        /// <summary>
        /// 获取分页的全部信息
        /// </summary>
        /// <param name="sitemapid"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<TextInfo> GetPagedListBySiteMapID(string sitemapid, Hashtable searchParams, int pageindex, int pagesize, ref int count)
        {
            Hashtable ht = new Hashtable();
            if (searchParams != null)
            {
                //传递查询参数
                foreach (DictionaryEntry item in searchParams)
                {
                    ht.Add(item.Key, item.Value);
                }
            }
            ht.Add("sitemapid", sitemapid);
            return dao.GetPagedList(ht, pageindex, pagesize, ref count);
        }
        /// <summary>
        /// 分页 按父栏目查询
        /// </summary>
        /// <param name="parentSitemapid"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<TextInfo> GetPagedListByParentSiteMapID(string parentSitemapid, Hashtable searchParams, int pageindex, int pagesize, ref int count)
        {
            Hashtable ht = new Hashtable();
            if (searchParams != null)
            {
                //传递查询参数
                foreach (DictionaryEntry item in searchParams)
                {
                    ht.Add(item.Key, item.Value);
                }
            }
            ht.Add("parentSitemapid", parentSitemapid);
            return dao.GetPagedList(ht, pageindex, pagesize, ref count);
        }



        /// <summary>
        ///  获取分页的可用信息
        /// </summary>
        /// <param name="sitemapid"></param>
        /// <param name="searchParams"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<TextInfo> GetActivePagedListBySiteMapID(string sitemapid, int pageindex, int pagesize, ref int count)
        {
            Hashtable ht = new Hashtable();
            ht.Add("sitemapid", sitemapid);
            ht.Add("orderby", "IsTop desc,PublishTime desc");
            return GetActivePagedListByParams(ht, pageindex, pagesize, ref count);
        }
        public IList<TextInfo> GetActivePagedReportBySiteMapID(string sitemapid, int year, int pageindex, int pagesize, ref int count)
        {
            Hashtable ht = new Hashtable();
            if (year != 0)
            {
                ht.Add("publishyear", year);
            }
            ht.Add("sitemapid", sitemapid);
            ht.Add("orderby", "IsTop desc,MediaPublishTime desc,CreateTime desc");
            return GetActivePagedListByParams(ht, pageindex, pagesize, ref count);
        }


        /// <summary>
        /// 可用 分页 按父栏目查询
        /// </summary>
        /// <param name="parentSitemapid"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<TextInfo> GetActivePagedListByParentSiteMapID(string parentSitemapid, int pageindex, int pagesize, ref int count)
        {
            Hashtable ht = new Hashtable();
            ht.Add("parentSitemapid", parentSitemapid);
            ht.Add("orderby", "IsTop desc,PublishTime desc");
            return GetActivePagedListByParams(ht, pageindex, pagesize, ref count);
        }

        #endregion

        #region 获取全部可用数据的分页列表（用于全文搜索）

        /// <summary>
        /// 获取全部可用数据的分页列表（用于全文搜索）
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<TextInfo> GetActivePagedList(int pageindex, int pagesize, ref int count)
        {
            Hashtable ht = new Hashtable();
            return GetActivePagedListByParams(ht, pageindex, pagesize, ref count);
        }

        #endregion

        #region 修改信息状态
        /// <summary>
        /// 修改信息状态
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="state"></param>
        public void ChangeState(string tid, UtilityEnum.State state)
        {
            TextInfo info = dao.GetDetail(tid);
            if (info != null)
            {
                info.State = (int)state;
                info.UpdateTime = DateTime.Now;

                //BLL中的Update事件也可能在前台记录浏览次数的时候被调用，而前台的调用不需要记录到日志中，所以日志要改在此处记录
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Update, GetDetail(info.TextInfoId));

                Update(info);
            }
        }
        #endregion

        #endregion

        #region 判断标题是否已经存在
        /// <summary>
        /// 判断标题是否已经存在
        /// </summary>
        /// <param name="p"></param>
        /// <param name="tid"></param>
        /// <returns></returns>
        public bool TitleExists(string title, string tid)
        {
            Hashtable ht = new Hashtable();
            ht.Add("fullTitle", title);
            IList<TextInfo> list = dao.GetListByParams(ht);

            if (list != null && list.Count > 0)
            {
                foreach (TextInfo info in list)
                {
                    if (info.TextInfoId != tid)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        #endregion

        #region 获取资源文件的路径

        /// <summary>
        /// 获取资源文件的路径 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public string GetSourceDir()
        {
            return "/UploadFile/Source/" + DateTime.Now.ToString("yyyy/MM/dd/");
        }

        #endregion

        #region 根据标题返回文字信息

        /// <summary>
        /// 根据标题返回文字信息
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public TextInfo GetActiveDetailByTitle(string title)
        {
            Hashtable ht = new Hashtable();
            ht.Add("fullTitle", title);

            IList<TextInfo> list = GetActiveListByParams(ht);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

        #endregion

        #region 根据关键字获取相关的视频

        /// <summary>
        /// 根据关键字获取相关的视频
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IList<TextInfo> GetRelatedVideo(TextInfo info)
        {
            //取所有关键字
            IList<Keyword> keylist = KeywordBLL.Instance.GetListByTextInfoId(info.TextInfoId);

            //取有相同关键字的视频信息,且排除当前ID
            Hashtable ht = new Hashtable();
            if (keylist != null && keylist.Count != 0)
            {
                ht.Add("keylist", keylist);
            }
            ht.Add("max", 4);
            ht.Add("currentId", info.TextInfoId);
            ht.Add("sitemapId", info.SitemapId);
            ht.Add("state", (int)UtilityEnum.State.Enable);
            ht.Add("publishTime", DateTime.Now);

            return dao.GetRelatedVideoList(ht);
        }

        #endregion

        #region 搜索

        /// <summary>
        /// 搜索
        /// </summary>
        /// <returns></returns>
        public IList<TextInfo> SearchInfo(string parentSitemapId, string param, int pageindex, int pagesize, ref int count)
        {
            IList<TextInfo> list = new List<TextInfo>();
            //按关键字
            Keyword key = KeywordBLL.Instance.GetDetailByKey(param);
            Hashtable htKey = new Hashtable();
            if (key != null)
            {
                htKey.Add("keyId", key.KeyId);
            }
            if (parentSitemapId != "")
            {
                htKey.Add("parentSitemapid", parentSitemapId);
            }
            htKey.Add("param", param);
            htKey.Add("state", (int)UtilityEnum.State.Enable);
            htKey.Add("publishTime", DateTime.Now);

            return dao.GetPagedListByKey(htKey, pageindex, pagesize, ref count);
        }

        #endregion

        #region 上一条记录和下一条记录

        /// <summary>
        /// 获取上一条记录
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public TextInfo GetPrevRecord(TextInfo Info)
        {
            if (Info != null)
            {
                Hashtable ht = new Hashtable();
                ht.Add("sitemapid", Info.SitemapId);
                ht.Add("textinfoid", Info.TextInfoId);
                if (Info.IsFromMedia)
                {
                    ht.Add("orderby", "IsTop desc,MediaPublishTime desc");
                }
                ht.Add("prev", true);
                return dao.GetPrevOrNextRecord(ht);
            }
            return null;
        }

        /// <summary>
        /// 获取下一条记录
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public TextInfo GetNextRecord(TextInfo Info)
        {
            if (Info != null)
            {
                Hashtable ht = new Hashtable();
                ht.Add("sitemapid", Info.SitemapId);
                ht.Add("textinfoid", Info.TextInfoId);
                if (Info.IsFromMedia)
                {
                    ht.Add("orderby", "IsTop desc,MediaPublishTime desc");
                }
                return dao.GetPrevOrNextRecord(ht);
            }
            return null;
        }

        #endregion



        //获取可用列表
        private IList<TextInfo> GetActiveListByParams(Hashtable ht)
        {
            ht.Add("state", (int)UtilityEnum.State.Enable);//未禁用
            ht.Add("isDel", (int)UtilityEnum.Del.No);//未删除
            ht.Add("publishTime", DateTime.Now);//发布时间小于当前时间
            return dao.GetListByParams(ht);
        }
        //获取可用分页列表
        private IList<TextInfo> GetActivePagedListByParams(Hashtable ht, int pageindex, int pagesize, ref int count)
        {
            ht.Add("state", (int)UtilityEnum.State.Enable);//未禁用
            ht.Add("isDel", (int)UtilityEnum.Del.No);//未删除
            ht.Add("publishTime", DateTime.Now);//发布时间小于当前时间
            return dao.GetPagedList(ht, pageindex, pagesize, ref count);
        }


    }
}
